package com.dingding.controller;

import cn.hutool.json.JSONUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSort;
import com.dingding.controller.BaseController;
import com.dingding.entity.request.user.*;
import com.dingding.entity.BaseResponse;
import com.dingding.entity.response.user.*;
import com.dingding.enums.ResponseCode;
import com.dingding.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@Slf4j
@RestController
@RequestMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Api(value = "登录认证", tags = {"1. 登录认证"})
public class LoginController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 登录请求，提交用户ID和手机验证码
     *
     * @author DingXintao
     * @date 2023/8/23 10:34
     * @param req
     * @return com.marketingtool.controller.response.BaseResponse<com.marketingtool.controller.response.user.LoginResponse>
     **/
    @ApiOperationSort(1)
    @ApiOperation(value = "1. 登录请求")
    @PostMapping("user/login")
    public BaseResponse<LoginResponse> login(@RequestBody @Validated LoginRequest req) throws Exception{
        log.info("=======enter /user/login encryptReq:{}", JSONUtil.toJsonStr(req));
        return userService.login(req);
    }

    /**
     * 获取图片验证码，生产uuid和图片字符串，并存储redis供后面接口对比验证
     *
     * @author DingXintao
     * @date 2023/8/23 10:34
     * @return com.marketingtool.controller.response.BaseResponse<com.marketingtool.controller.response.user.CaptchResponse>
     **/
    @ApiOperationSort(2)
    @ApiOperation(value = "2. 获取图片验证码")
    @PostMapping("user/captch")
    public BaseResponse<CaptchResponse> captch() {
        log.info("=======enter /user/captch");
        BaseResponse res;
        CaptchResponse captchResponse = userService.captch();
        if (Objects.nonNull(captchResponse)) {
            res = new BaseResponse(ResponseCode.SUCCESS, captchResponse);
        } else {
            res = new BaseResponse(ResponseCode.FAILURE);
        }
        return res;
    }

    /**
     * 获取用户信息：姓名，邮箱，企业，工号，省，头像地址
     *
     * @author DingXintao
     * @date 2023/8/23 11:07
     * @return com.marketingtool.controller.response.BaseResponse<com.marketingtool.controller.response.user.EncryptResponse>
     **/
    @ApiOperationSort(5)
    @ApiOperation(value = "5. 获取用户信息", notes = "姓名，邮箱，企业，工号，省，头像地址等")
    @PostMapping("user/info")
    public BaseResponse<UserInfoResponse> info() throws Exception {
        log.info("=======enter /user/info");

        UserInfoResponse userResp = userService.info();
        return new BaseResponse(ResponseCode.SUCCESS, userResp);
    }

    @ApiOperationSort(6)
    @ApiOperation(value = "6. 退出登录")
    @PostMapping("user/logout")
    public BaseResponse logout() {
        log.info("=======enter /user/logout");
        userService.logout();
        return new BaseResponse(ResponseCode.SUCCESS);
    }


}
